import 'package:flutter/material.dart';

/// 基础页面
///
/// 这是一个页面基础组件
class BasePage extends StatefulWidget {
  /// 标题
  final String? title;

  /// appbar优先级高于title
  final PreferredSizeWidget? appBar;

  /// 内容
  final Widget child;

  /// appbar右侧功能按钮
  final List<Widget>? actions;

  /// 返回键和页面返回按钮
  final Future<bool> Function()? onWillPop;

  /// 悬浮按钮
  final Widget? floatingActionButton;

  /// 左侧抽屉组件
  final Widget? drawer;

  /// 右侧抽屉组件
  final Widget? endDrawer;

  /// 底部导航栏
  final Widget? bottomNavigationBar;

  /// 底部上拉框
  final Widget? bottomSheet;

  /// 背景色
  final Color? backgroundColor;

  const BasePage({
    Key? key,
    this.title,
    this.appBar,
    required this.child,
    this.actions,
    this.onWillPop,
    this.floatingActionButton,
    this.drawer,
    this.endDrawer,
    this.bottomNavigationBar,
    this.bottomSheet,
    this.backgroundColor,
  }) : super(key: key);

  @override
  _BasePageState createState() => _BasePageState();
}

class _BasePageState extends State<BasePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getAppBar(),
      body: getBody(),
      floatingActionButton: widget.floatingActionButton,
      drawer: widget.drawer,
      endDrawer: widget.endDrawer,
      bottomNavigationBar: widget.bottomNavigationBar,
      bottomSheet: widget.bottomSheet,
      backgroundColor: widget.backgroundColor,
    );
  }

  /// 获取body
  Widget getBody() {
    return WillPopScope(
      onWillPop: widget.onWillPop,
      child: widget.child,
    );
  }

  /// 获取appbar
  PreferredSizeWidget? getAppBar() {
    if (widget.appBar != null) return widget.appBar;
    if (widget.title != null || widget.actions != null)
      return AppBar(
        title: Text(widget.title ?? ''),
        actions: widget.actions,
      );

    return null;
  }
}


